package com.jcl.elasticsearch.resume.logic;

import com.jcl.core.utils.CollectionUtils;
import com.jcl.elasticsearch.core.property.ElasticConditionProperty;
import com.jcl.elasticsearch.resume.model.*;
import com.jcl.elasticsearch.resume.po.ResumeResultEntity;
import com.jcl.elasticsearch.resume.po.ResumeSimpleResultEntity;
import com.jcl.elasticsearch.resume.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.util.CollectionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ResumeCrawlerExportLogic
 * @Description TODO
 * @Author yyliu
 * @Date 2018/11/13 10:20
 * @Version 1.0
 **/
@Component
public class ResumeCommonLogic {

    @Autowired
    private SearchResumeBasicInfoLogic searchResumeBasicInfoLogic;

    @Autowired
    private SearchResumePersInfoLogic searchResumePersInfoLogic;

    @Autowired
    private SearchResumeSelfEvaluationLogic searchResumeSelfEvaluationLogic;

    @Autowired
    private SearchResumeJobIntentionLogic searchResumeJobIntentionLogic;

    @Autowired
    private SearchResumeWorkLogic searchResumeWorkLogic;

    @Autowired
    private SearchResumeEduLogic searchResumeEduLogic;

    @Autowired
    private SearchResumeProjectLogic searchResumeProjectLogic;


    public List<ResumeSimpleResultEntity> findSimpleList(List<Integer> resumeIds) {
        List<ResumeSimpleResultEntity> list = new ArrayList<>();
        List<SearchResumeBasicInfo> entityList = searchResumeBasicInfoLogic.findList(resumeIds);
        if(CollectionUtils.isNotEmpty(entityList)){

            Map<Integer, SearchResumeJobIntention> jobIntentionMap =
                    searchResumeJobIntentionLogic.findEntityMapByResumeIds(resumeIds);
            Map<Integer, SearchResumePersInfo> persInfoMap =
                    searchResumePersInfoLogic.findEntityMapByResumeIds(resumeIds);

            for(SearchResumeBasicInfo searchResumeBasicInfo: entityList){

                ResumeSimpleResultEntity resumeResultEntity = new ResumeSimpleResultEntity();

                SearchResumePersInfo persInfo = persInfoMap.get(searchResumeBasicInfo.getId());
                SearchResumeJobIntention jobIntention = jobIntentionMap.get(searchResumeBasicInfo.getId());
                //
                BeanUtils.copyProperties(persInfo, resumeResultEntity);
                BeanUtils.copyProperties(jobIntention, resumeResultEntity);

                resumeResultEntity.setApplyDate(searchResumeBasicInfo.getApplyDate());
                resumeResultEntity.setUpdateDate(searchResumeBasicInfo.getUpdateDate());
                resumeResultEntity.setApplyDepartment(searchResumeBasicInfo.getApplyDepartment());
                resumeResultEntity.setApplyPosition(searchResumeBasicInfo.getApplyPosition());
                resumeResultEntity.setSourceSub(searchResumeBasicInfo.getSourceSub());
                resumeResultEntity.setStatus(searchResumeBasicInfo.getStatus());

                resumeResultEntity.setId(searchResumeBasicInfo.getId());

                list.add(resumeResultEntity);
            }
        }

        return list;
    }


    public List<ResumeResultEntity> findList(List<Integer> resumeIds) {
        List<ResumeResultEntity> list = new ArrayList<>();
        List<SearchResumeBasicInfo> entityList = searchResumeBasicInfoLogic.findList(resumeIds);
        if(CollectionUtils.isNotEmpty(entityList)){

            Map<Integer, SearchResumePersInfo> persInfoMap =
                    searchResumePersInfoLogic.findEntityMapByResumeIds(resumeIds);
            Map<Integer, SearchResumeSelfEvaluation> selfEvaluationMap =
                    searchResumeSelfEvaluationLogic.findEntityMapByResumeIds(resumeIds);
            Map<Integer, SearchResumeJobIntention> jobIntentionMap =
                    searchResumeJobIntentionLogic.findEntityMapByResumeIds(resumeIds);
            Map<Integer, List<SearchResumeWork>> worksMap =
                    searchResumeWorkLogic.findListByResumeMap(resumeIds);
            Map<Integer, List<SearchResumeEdu>> edusMap =
                    searchResumeEduLogic.findListByResumeMap(resumeIds);
            Map<Integer, List<SearchResumeProject>> projectsMap =
                    searchResumeProjectLogic.findListByResumeMap(resumeIds);

            for(SearchResumeBasicInfo searchResumeBasicInfo: entityList){

                ResumeResultEntity resumeResultEntity = new ResumeResultEntity();

                SearchResumePersInfo persInfo = persInfoMap.get(searchResumeBasicInfo.getId());
                SearchResumeSelfEvaluation searchResumeSelfEvaluation =
                        selfEvaluationMap.get(searchResumeBasicInfo.getId());
                SearchResumeJobIntention jobIntention = jobIntentionMap.get(searchResumeBasicInfo.getId());
                List<SearchResumeWork> works = worksMap.get(searchResumeBasicInfo.getId());
                List<SearchResumeEdu> edus = edusMap.get(searchResumeBasicInfo.getId());
                List<SearchResumeProject> projects = projectsMap.get(searchResumeBasicInfo.getId());

                resumeResultEntity.setBasicInfo(persInfo);
                resumeResultEntity.setSelfEvaluation(searchResumeSelfEvaluation);
                resumeResultEntity.setJobIntention(jobIntention);
                resumeResultEntity.setWorks(works);
                resumeResultEntity.setEducations(edus);
                resumeResultEntity.setProjects(projects);

                resumeResultEntity.setApplyDate(searchResumeBasicInfo.getApplyDate());
                resumeResultEntity.setUpdateDate(searchResumeBasicInfo.getUpdateDate());
                resumeResultEntity.setApplyDepartment(searchResumeBasicInfo.getApplyDepartment());
                resumeResultEntity.setApplyPosition(searchResumeBasicInfo.getApplyPosition());
                resumeResultEntity.setSourceSub(searchResumeBasicInfo.getSourceSub());
                resumeResultEntity.setStatus(searchResumeBasicInfo.getStatus());

                resumeResultEntity.setId(searchResumeBasicInfo.getId());

                list.add(resumeResultEntity);
            }
        }

        return list;
    }

    public ResumeSimpleResultEntity findSimpleEntity(Integer resumeId) {

        ResumeSimpleResultEntity resumeResultEntity = new ResumeSimpleResultEntity();
        SearchResumeBasicInfo searchResumeBasicInfo = searchResumeBasicInfoLogic.findOne(resumeId);

        SearchResumePersInfo persInfo = searchResumePersInfoLogic.findOne(searchResumeBasicInfo.getId());
        SearchResumeJobIntention jobIntention = searchResumeJobIntentionLogic.findOne(searchResumeBasicInfo.getId());
        //
        BeanUtils.copyProperties(persInfo, resumeResultEntity);
        BeanUtils.copyProperties(jobIntention, resumeResultEntity);

        resumeResultEntity.setApplyDate(searchResumeBasicInfo.getApplyDate());
        resumeResultEntity.setUpdateDate(searchResumeBasicInfo.getUpdateDate());
        resumeResultEntity.setApplyDepartment(searchResumeBasicInfo.getApplyDepartment());
        resumeResultEntity.setApplyPosition(searchResumeBasicInfo.getApplyPosition());
        resumeResultEntity.setSourceSub(searchResumeBasicInfo.getSourceSub());
        resumeResultEntity.setStatus(searchResumeBasicInfo.getStatus());

        resumeResultEntity.setId(searchResumeBasicInfo.getId());
        return resumeResultEntity;
    }

    public ResumeResultEntity findDetailEntity(Integer resumeId) {

        return findOne(resumeId, false);
    }

    public ResumeResultEntity findOne(Integer resumeId, boolean isClearDuty) {
        ResumeResultEntity resumeResultEntity = null;

        SearchResumeBasicInfo searchResumeBasicInfo = searchResumeBasicInfoLogic.findOne(resumeId);
        if(null != searchResumeBasicInfo){
            resumeResultEntity = new ResumeResultEntity();
            SearchResumePersInfo persInfo = searchResumePersInfoLogic.findOne(resumeId);
            SearchResumeSelfEvaluation searchResumeSelfEvaluation = searchResumeSelfEvaluationLogic.findOne(resumeId);
            SearchResumeJobIntention jobIntention = searchResumeJobIntentionLogic.findOne(resumeId);
            List<SearchResumeWork> works = searchResumeWorkLogic.findListByResumeId(resumeId);
            List<SearchResumeEdu> edus = searchResumeEduLogic.findListByResumeId(resumeId);
            List<SearchResumeProject> projects = searchResumeProjectLogic.findListByResumeId(resumeId);

            if(isClearDuty){
                if(CollectionUtils.isNotEmpty(works)){
                    for(SearchResumeWork entity: works){
                        entity.setDuty(null);
                    }
                }
                if(CollectionUtils.isNotEmpty(projects)){
                    for(SearchResumeProject entity: projects){
                        entity.setDuty(null);
                    }
                }
            }
            resumeResultEntity.setBasicInfo(persInfo);
            resumeResultEntity.setSelfEvaluation(searchResumeSelfEvaluation);
            resumeResultEntity.setJobIntention(jobIntention);
            resumeResultEntity.setWorks(works);
            resumeResultEntity.setEducations(edus);
            resumeResultEntity.setProjects(projects);

            resumeResultEntity.setApplyDate(searchResumeBasicInfo.getApplyDate());
            resumeResultEntity.setUpdateDate(searchResumeBasicInfo.getUpdateDate());
            resumeResultEntity.setApplyDepartment(searchResumeBasicInfo.getApplyDepartment());
            resumeResultEntity.setApplyPosition(searchResumeBasicInfo.getApplyPosition());
            resumeResultEntity.setSourceSub(searchResumeBasicInfo.getSourceSub());
            resumeResultEntity.setStatus(searchResumeBasicInfo.getStatus());

            resumeResultEntity.setId(searchResumeBasicInfo.getId());
        }

        return resumeResultEntity;
    }

    public Map<String, Object> findOneMap(Integer resumeId, boolean isClearDuty) {

        Map<String, Object> dataMap = searchResumeBasicInfoLogic.findOneMap(resumeId);
        if(null != dataMap){
            Map<String, Object> persInfo = searchResumePersInfoLogic.findOneMap(resumeId);
            Map<String, Object> searchResumeSelfEvaluation = searchResumeSelfEvaluationLogic.findOneMap(resumeId);
            Map<String, Object> jobIntention = searchResumeJobIntentionLogic.findOneMap(resumeId);

            persInfo.remove("id");
            persInfo.remove("resume_id");
            searchResumeSelfEvaluation.remove("id");
            searchResumeSelfEvaluation.remove("resume_id");
            jobIntention.remove("id");
            jobIntention.remove("resume_id");
            dataMap.putAll(persInfo);
            dataMap.putAll(searchResumeSelfEvaluation);
            dataMap.putAll(jobIntention);

            List<Map<String, Object>> works = searchResumeWorkLogic.findObjListByResumeId(resumeId);
            List<Map<String, Object>> edus = searchResumeEduLogic.findObjListByResumeId(resumeId);
            List<Map<String, Object>> projects = searchResumeProjectLogic.findObjListByResumeId(resumeId);

            if(CollectionUtils.isNotEmpty(works)){
                for(Map<String, Object> map: works){
                    map.remove("id");
                    map.remove("resume_id");
                    if(isClearDuty) {
                        map.remove("duty");
                    }
                }
            }
            if(CollectionUtils.isNotEmpty(edus)){
                for(Map<String, Object> map: edus){
                    map.remove("id");
                    map.remove("resume_id");
                }
            }
            if(CollectionUtils.isNotEmpty(projects)){
                for(Map<String, Object> map: projects){
                    if(isClearDuty) {
                        map.remove("duty");
                        map.remove("description");
                    }
                    map.remove("id");
                    map.remove("resume_id");
                }
            }
            dataMap.put(ElasticConditionProperty.RESUME_WORKS, works);
            dataMap.put(ElasticConditionProperty.RESUME_EDUCATIONS, edus);
            dataMap.put(ElasticConditionProperty.RESUME_PROJECTS, projects);
        }

        return dataMap;
    }




}
